<template>
    <div class="new-part">
        <div class="article-header">
            <h2>最新文章</h2>
        </div>
        <div v-loading="loading" class="article-list">
            <el-card v-for="(item, index) in infoList" :key="index" class="article-item" shadow="hover">
                <div class="article-content" @click="handleArticleClick(item)">
                    <div class="article-cover" v-if="item.cover">
                        <img :src="item.cover" :alt="item.title">
                    </div>
                    <div class="article-info">
                        <h3 class="article-title">{{ item.title }}</h3>
                        <p class="article-summary">{{ item.content }}</p>
                        <div class="article-footer">
                            <span class="article-time">
                                <i class="el-icon-time"></i>
                                {{ item.createTime }}
                            </span>
                            <span class="article-views">
                                <i class="el-icon-view"></i>
                                {{ item.viewNum || 0 }} 阅读
                            </span>
                            <span class="article-views">
                                <i class="iconfont icon-dianzanliang"></i>
                                {{ item.likeNum || 0 }} 点赞
                            </span>
                        </div>
                    </div>
                </div>
            </el-card>
            <div class="pagination-container">
                <el-pagination
                    @size-change="handleSizeChange"
                    @current-change="handleCurrentChange"
                    :current-page="pageNum"
                    :page-size="pageSize"
                    :page-sizes="[5, 10, 20, 50]"
                    layout="total, prev, pager, next"
                    :total="total">
                </el-pagination>
            </div>
        </div>
    </div>
</template>

<script>
import { reqArticleList } from '@/api'
export default {
    name: "new-part",
    data() {
        return {
            activeName: "2",
            loading: false,
            infoList: [],
            pageNum: 1,
            pageSize: 5,
            total: 0
        }
    },
    mounted() {
        this.getArticle();
    },
    methods: {
        getArticle() {
            this.loading = true;
            reqArticleList({
                pageNum: this.pageNum,
                pageSize: this.pageSize
            }).then(res => {
                this.infoList = res.rows;
                this.total = res.total;
                this.loading = false;
            }).catch(error => {
                this.loading = false;
            })
        },
        handleArticleClick(article) {
            // 处理文章点击事件
            this.$router.push(`/article/detail/${article.id}`);
        },
        handleSizeChange(val) {
            this.pageSize = val;
            this.pageNum = 1;
            this.getArticle();
        },
        handleCurrentChange(val) {
            this.pageNum = val;
            this.getArticle();
        }
    },
    watch: {
        activeName(newValue, oldValue) {
            this.loading = true;
            this.infoList = []
            this.judge(newValue);
        }
    }
}
</script>

<style scoped>
.new-part {
    padding: 20px;
    background: #fff;
    border-radius: 8px;
}

.article-header {
    margin-bottom: 20px;
}

.article-header h2 {
    font-size: 20px;
    color: #303133;
    margin: 0;
    padding-bottom: 10px;
    border-bottom: 2px solid #409EFF;
}

.article-list {
    height: calc(100% - 60px);
    overflow-y: auto;
}

.article-item {
    margin-bottom: 15px;
    cursor: pointer;
    transition: all 0.3s;
}

.article-content {
    padding: 10px;
    display: flex;
    gap: 15px;
}

.article-cover {
    flex-shrink: 0;
    width: 200px;
    height: 120px;
    overflow: hidden;
    border-radius: 4px;
}

.article-cover img {
    width: 100%;
    height: 100%;
    object-fit: cover;
    transition: transform 0.3s;
}

.article-cover img:hover {
    transform: scale(1.05);
}

.article-info {
    flex: 1;
    display: flex;
    flex-direction: column;
    justify-content: space-between;
}

.article-title {
    font-size: 16px;
    color: #303133;
    margin: 0 0 10px 0;
    font-weight: 600;
}

.article-summary {
    font-size: 14px;
    color: #606266;
    margin: 0 0 10px 0;
    line-height: 1.5;
    display: -webkit-box;
    -webkit-box-orient: vertical;
    -webkit-line-clamp: 2;
    overflow: hidden;
    flex: 1;
}

.article-footer {
    display: flex;
    align-items: center;
    font-size: 12px;
    color: #909399;
}

.article-time,
.article-views {
    margin-right: 15px;
    display: flex;
    align-items: center;
}

.article-time i,
.article-views i {
    margin-right: 4px;
}

/* 自定义滚动条样式 */
.article-list::-webkit-scrollbar {
    width: 6px;
}

.article-list::-webkit-scrollbar-thumb {
    background: #dcdfe6;
    border-radius: 3px;
}

.article-list::-webkit-scrollbar-track {
    background: #f5f7fa;
}

.pagination-container {
    display: flex;
    justify-content: center;
    margin-top: 10px;
}
</style>